<template>
  <div class="page-box">
    <el-card class="page-box-card">
      <el-form
        :model="queryParams"
        ref="queryForm"
        size="small"
        label-width="80px"
        inline
        @submit.native.prevent
      >
        <el-form-item label="菜单名称" prop="param.name">
          <el-input
            v-model="queryParams.param.name"
            placeholder="请输入菜单名称"
            clearable
            style="width: 240px"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="档案类型" prop="param.type">
          <el-select
            style="width: 240px"
            v-model="queryParams.param.type"
            placeholder="请选择档案类型"
          >
            <el-option label="筛查" value="0"> </el-option>
            <el-option label="急救" value="1"> </el-option>
            <el-option label="治疗" value="2"> </el-option>
            <el-option label="康复" value="3"> </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="small"
            @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="el-icon-refresh" size="small" @click="resetQuery"
            >重置</el-button
          >
        </el-form-item>
      </el-form>
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['definition:class:add']"
            >新增</el-button
          >
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleEdit"
            v-hasPermi="['definition:class:edit']"
            >修改</el-button
          >
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['definition:class:delete']"
            >删除</el-button
          >
        </el-col>
        <!-- <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-upload2"
            size="mini"
            v-hasPermi="['definition:class:upload']"
            >导入</el-button
          >
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            v-hasPermi="['definition:class:download']"
            >导出</el-button
          >
        </el-col> -->
      </el-row>
      <el-table
        v-loading="loading"
        :data="dataList"
        style="width: 100%"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center" fixed />
        <el-table-column
          prop="sort"
          label="排序"
          width="60"
          align="center"
          fixed
        />
        <el-table-column
          prop="name"
          label="菜单名称"
          width="180"
          align="center"
          fixed
        >
        </el-table-column>
        <el-table-column
          prop="remark"
          label="档案类型"
          width="180"
          align="center"
        >
          <template slot-scope="scope">
            <span v-if="scope.row.type == 0">筛查</span>
            <span v-else-if="scope.row.type == 1">急救</span>
            <span v-else-if="scope.row.type == 2">治疗</span>
            <span v-else-if="scope.row.type == 3">康复</span>
          </template>
        </el-table-column>
        <el-table-column label="变量" prop="codeList">
          <template slot-scope="scope">
            <span v-for="(item, index) in scope.row.codeList" :key="index">
              {{ item.codeName }}
              <span v-if="index < scope.row.codeList.length - 1">，</span>
            </span>
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          align="center"
          fixed="right"
          width="160px"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleEdit(scope.row)"
              v-hasPermi="['system:dept:edit']"
              >修改</el-button
            >
            <el-button
              size="mini"
              type="text"
              style="color: red"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['system:dept:edit']"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-card>
    <!-- 添加或修改部门对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="addDialog"
      width="600px"
      append-to-body
    >
      <el-form ref="form" :model="addForm" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="菜单名称" prop="name">
              <el-input
                style="width: 100%"
                v-model="addForm.name"
                placeholder="请输入菜单名称"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="档案类型" prop="type">
              <el-select
                style="width: 100%"
                v-model="addForm.type"
                placeholder="请选择档案类型"
              >
                <el-option label="筛查" :value="0"> </el-option>
                <el-option label="急救" :value="1"> </el-option>
                <el-option label="治疗" :value="2"> </el-option>
                <el-option label="康复" :value="3"> </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="变量" prop="type">
              <el-select
                v-model="addForm.codes"
                multiple
                filterable
                style="width: 100%"
                placeholder="请选择"
              >
                <el-option
                  v-for="(item, index) in codeList"
                  :key="index"
                  :label="item.name"
                  :value="item.code"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="排序" prop="sort">
              <el-input-number
                style="width: 100%"
                v-model="addForm.sort"
                placeholder="请输入排序"
              ></el-input-number>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { getListCode } from "@/api/business/definition";
import { getListCms, changeCms, delCms } from "@/api/business/cms";
export default {
  data() {
    return {
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      selectionData: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        param: {
          name: null,
          type: null,
        },
      },
      codeList: [],
      dataList: [],
      total: 0,
      title: "",
      addDialog: false,
      addForm: {
        name: "",
      },
      rules: {
        name: [{ required: true, message: "请输入菜单名称", trigger: "blur" }],
        type: [
          { required: true, message: "请选择所属档案类型", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
    this.getCodeList();
  },
  methods: {
    // 获取数据列表
    getCodeList() {
      const param = {
        pageNum: -1,
        param: {
          text: "",
          code: "",
          tagIdList: [],
        },
      };
      getListCode(param).then((response) => {
        this.codeList = response.data.list;
      });
    },
    // 搜索事件
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    // 重置事件
    resetQuery() {
      this.dataList = [];
      this.total = 0;
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 获取数据列表
    getList() {
      this.loading = true;
      const param = { ...this.queryParams };
      getListCms(param).then((res) => {
        const { data } = res;
        this.dataList = data.list;
        this.total = data.total;
        this.loading = false;
      });
    },
    // 列表选中事件
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.selectionData = [...selection];
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    // 新增标签
    handleAdd() {
      this.title = "新增标签";
      this.addDialog = true;
    },
    // 新增/修改弹框提交事件
    submitForm() {
      const param = { ...this.addForm };
      param.codeList = [...param.codes];
      // 修改
      const text = param.id ? "修改" : "新增";
      changeCms(param).then((res) => {
        this.$message.success(`${text}成功`);
        this.cancel();
        this.handleQuery();
      });
    },
    // 新增/修改弹框取消事件
    cancel() {
      this.addForm = {
        name: "",
        sort: 0,
      };
      this.addDialog = false;
    },
    // 修改标签
    handleEdit(row) {
      this.title = "修改标签";
      const data = row.id ? row : this.selectionData[0];
      this.addForm = {
        ...data,
        codes: data.codeList.map((item) => {
          return item.code;
        }),
      };
      this.addDialog = true;
    },
    // 删除标签
    handleDelete(row) {
      const idList = row.id ? [row.id] : [...this.ids];
      this.$modal
        .confirm("是否确认删除当前选择的标签？")
        .then(function () {
          // 删除
          return delCms({ idList });
        })
        .then(() => {
          this.$message.success("删除成功");
          this.handleQuery();
        })
        .catch(() => {});
    },
  },
};
</script>
